package com.yyairmarkyy.arctool.window;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;


import com.yyairmarkyy.arctool.customview.ArcToolRecyclerview;
import com.yyairmarkyy.arctool.customview.ArcToolRootLyt;
import com.yyairmarkyy.arctool.customview.CenterView;
import com.yyairmarkyy.arctool.customview.MenuToolLayout;
import com.yyairmarkyy.arctool.inter.ArcOnKeyLisitener;
import com.yyairmarkyy.arctool.inter.ArcOutsideClickLisitener;
import com.yyairmarkyy.arctool.inter.CenterViewClickListener;
import com.yyairmarkyy.arctool.item.ArcFadeItem;
import com.yyairmarkyy.arctool.item.ArcLevelItem;
import com.yyairmarkyy.arctool.item.AiyouArcMenuItem;
import com.yyairmarkyy.arctool.layoutmanager.CircleLayoutManager;
import com.yyairmarkyy.arctool.layoutmanager.core.CenterScrollListener;
import com.yyairmarkyy.arctool.util.OnKeyHomeRecevier;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by aiyou on 2017/10/18.
 */

public class ArcToolController implements View.OnClickListener{

    private Context mContext;
    private ArcToolWindow arcToolWindow;
    private WindowManager windowManager;
    private List<ArcToolRecyclerview> arcToolRecyclerviews;
    private ArcToolRootLyt rootLyt;
    private MenuToolLayout menuToolLayout;

    private int allLevel;
    private int maxRadius;
    private int centerViewBackgroundColor;
    private int centerViewIconResid;
    private int centerViewRadius;
    private List<ArcFadeItem> fadeItems;
    private List<ArcLevelItem> levelItems;
    private CenterView centerView;
    private AiyouArcMenuItem menuItem;

    private CenterViewClickListener centerViewClickListener;
    public ArcOutsideClickLisitener outsideClickLisitener;

    public ArcOnKeyLisitener onKeyLisitener;

    private WindowManager.LayoutParams layoutParams;
    private OnKeyHomeRecevier onKeyHomeRecevier;

    public boolean isShow;



    public ArcToolController(Context mContext, ArcToolWindow toolWindow, WindowManager windowManager, WindowManager.LayoutParams layoutParams) {
        this.mContext = mContext;
        arcToolWindow = toolWindow;
        this.windowManager = windowManager;
        this.layoutParams = layoutParams;
        //初始化根布局
        rootLyt = new ArcToolRootLyt(mContext);
        rootLyt.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        //初始化底部
        centerView = new CenterView(mContext);

        //初始化recycleview
        arcToolRecyclerviews = new ArrayList<>();
    }


    public void setAllLevel(int allLevel) {
        this.allLevel = allLevel;
        for (int i = 0;i<allLevel;i++){
            ArcToolRecyclerview arcToolRecyclerview = new ArcToolRecyclerview(mContext);
            arcToolRecyclerviews.add(arcToolRecyclerview);
        }
    }

    public void setMaxRadius(int maxRadius) {
        this.maxRadius = maxRadius;
        rootLyt.setBgRadius(maxRadius);
    }

    public void setCenterViewBackgroundColor(int centerViewBackgroundColor) {
        this.centerViewBackgroundColor = centerViewBackgroundColor;
        centerView.setBgColor(centerViewBackgroundColor);
    }

    public void setCenterViewIconResid(int centerViewIconResid,int size,Point iconPosition) {
        this.centerViewIconResid = centerViewIconResid;
        centerView.setIcon(centerViewIconResid,size,iconPosition);
    }

    public void setCenterViewRadius(int centerViewRadius) {
        this.centerViewRadius = centerViewRadius;
        centerView.setLayoutParams(new FrameLayout.LayoutParams(centerViewRadius,centerViewRadius));
        centerView.setIconSize(centerViewRadius/2);
    }


    public void setCenterViewClickListener(CenterViewClickListener centerViewClickListener) {
        this.centerViewClickListener = centerViewClickListener;
        centerView.setOnClickListener(this);
    }

    public void setBgColor(int bgColor){
        rootLyt.setBgColor(bgColor);
    }

    public void setBgAlpha(int alpha){
        rootLyt.setBgAlpha(alpha);
    }

    public void setFadeItems(List<ArcFadeItem> fadeItems) {
        this.fadeItems = fadeItems;
        rootLyt.setFadeItems(fadeItems);
    }

    public void setLevelItems(List<ArcLevelItem> levelItems) {
        this.levelItems = levelItems;
    }


    public void setMenuItem(AiyouArcMenuItem menuItem) {
        this.menuItem = menuItem;
    }

    public void setOutsideClickLisitener(ArcOutsideClickLisitener outsideClickLisitener) {
        this.outsideClickLisitener = outsideClickLisitener;
    }

    public void hideRecyclerView(int index){
        if (arcToolRecyclerviews == null || arcToolRecyclerviews.size() == 0)
            return;

        arcToolRecyclerviews.get(index).setVisibility(View.GONE);
    }

    public void showRecyclerView(int index){
        if (arcToolRecyclerviews == null || arcToolRecyclerviews.size() == 0)
            return;

        arcToolRecyclerviews.get(index).setVisibility(View.VISIBLE);
    }

    public void setOnKeyLisitener(ArcOnKeyLisitener onKeyLisitener) {
        this.onKeyLisitener = onKeyLisitener;
    }

    public void setFadeVisiable(int index,@ArcFadeItem.FadeVisibility int visiable){
        fadeItems.get(index).setVisiable(visiable);
    }

    public int getLevelItemVisibility(int index){
        return arcToolRecyclerviews.get(index).getVisibility();

    }



    public void show(){

        if (isShow)
            return;

        makeRootLyt();
        windowManager.addView(rootLyt,layoutParams);
        isShow = true;

    }

    public void dismiss(){
        if (isShow) {
            windowManager.removeViewImmediate(rootLyt);
            mContext.unregisterReceiver(onKeyHomeRecevier);
            rootLyt.removeOutsideClickLisitener();
        }
        onKeyHomeRecevier = null;
        isShow = false;

    }

    /**
     * 更新根布局
     */
    public void updateRootlyt(){
        updateRootLyt();
        rootLyt.requestLayout();
        rootLyt.invalidate();
    }


    public void setWindowLayoutParams(){
        if (layoutParams != null)
            return;
        layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.TYPE_APPLICATION
                |WindowManager.LayoutParams.FLAG_DIM_BEHIND|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.dimAmount = 0.6f;
        layoutParams.format = PixelFormat.RGBA_8888;

    }



    @Override
    public void onClick(View v) {
        if (v == centerView){
            if (centerViewClickListener != null)
                centerViewClickListener.onClick();
        }
    }

    /**
     * 创建windowlyt
     */
    private void makeRootLyt(){


       if(rootLyt.getChildCount() != 0)
           return;


        // TODO: 2017/10/24 左右判断
        //生成并添加每个环形选项
        for (int i = 0; i<allLevel; i++){
            ArcLevelItem item = levelItems.get(i);
            ArcToolRecyclerview recyclerview = arcToolRecyclerviews.get(i);
            FrameLayout.LayoutParams frameLayoutP = new FrameLayout.LayoutParams(item.getTopRadius(),item.getTopRadius());
            CircleLayoutManager circleLayoutManager = (CircleLayoutManager) item.getLayoutManager();
            frameLayoutP.gravity = Gravity.RIGHT|Gravity.BOTTOM;
            if (item.getShowNum() != 0){
                if (item.getShowNum() == 3){
                    circleLayoutManager.setIntervalAngle(25);
                }else if (item.getShowNum() == 4){
                    circleLayoutManager.setIntervalAngle(20);
                }
            }
            circleLayoutManager.setShowNum(item.getShowNum());
            recyclerview.setLayoutManager(item.getLayoutManager());
            recyclerview.setOverScrollMode(View.OVER_SCROLL_NEVER);
            recyclerview.setLayoutParams(frameLayoutP);
            recyclerview.addOnScrollListener(new CenterScrollListener());
            rootLyt.addView(recyclerview,0);
            recyclerview.setAdapter(item.getAdapter());

//            recyclerview.scrollToPosition(item.getShowNum());
        }



        //生成并添加菜单布局
        if (menuToolLayout == null)
//            menuToolLayout.removeAllViews();
//        else
            menuToolLayout = new MenuToolLayout(mContext);

        FrameLayout.LayoutParams menuP = new FrameLayout.LayoutParams(menuItem.getTopRadius(),menuItem.getTopRadius());
        menuP.gravity = Gravity.RIGHT|Gravity.BOTTOM;
        menuToolLayout.setViewsPerRadius(menuItem.getPerViewRadius());
        RelativeLayout.LayoutParams perViewP = new RelativeLayout.LayoutParams(menuItem.getPerViewWidth(),menuItem.getPerViewHeight());
        for (View view : menuItem.getPerViews()){
            menuToolLayout.addView(view,0,perViewP);
        }
        rootLyt.addView(menuToolLayout,menuP);


        //添加关闭按钮布局
        FrameLayout.LayoutParams centerP = (FrameLayout.LayoutParams) centerView.getLayoutParams();
        if (centerP != null){
            centerP.gravity = Gravity.RIGHT|Gravity.BOTTOM;
        }

        rootLyt.addView(centerView,centerP);



        rootLyt.setFocusableInTouchMode(true);
        rootLyt.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                Log.e("arctool", "arctool_key:" + event.getAction());
                if (event.getKeyCode() == KeyEvent.KEYCODE_BACK || event.getKeyCode() == KeyEvent.KEYCODE_SETTINGS || event.getKeyCode() == KeyEvent.KEYCODE_MENU) {
                    if (onKeyLisitener != null)
                        onKeyLisitener.onKeyLisitener(event.getKeyCode());
                }
                return false;
            }
        });



        //创建广播
        onKeyHomeRecevier = new OnKeyHomeRecevier();
        //动态注册广播
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        //启动广播
        mContext.registerReceiver(onKeyHomeRecevier, intentFilter);
        onKeyHomeRecevier.addOnKeyHomeCallBack(new OnKeyHomeRecevier.OnKeyHomeCallBack() {
            @Override
            public void downHome() {
                if (onKeyLisitener != null)
                    onKeyLisitener.onKeyLisitener(KeyEvent.KEYCODE_HOME);
            }

            @Override
            public void downMenu() {
                if (onKeyLisitener != null)
                    onKeyLisitener.onKeyLisitener(KeyEvent.KEYCODE_MENU);
            }
        });





        //添加监听
        if (outsideClickLisitener != null)
            rootLyt.setOutsideClickLisitener(outsideClickLisitener);


    }



    private void updateRootLyt(){

        for (int i = 0; i<allLevel; i++){
            ArcLevelItem item = levelItems.get(i);
            ArcToolRecyclerview recyclerview = arcToolRecyclerviews.get(i);
            if (recyclerview.getAdapter() != item.getAdapter()){
                CircleLayoutManager circleLayoutManager = (CircleLayoutManager) item.getLayoutManager();
                if (item.getShowNum() != 0){
                    if (item.getShowNum() == 3){
                        circleLayoutManager.setIntervalAngle(25);
                    }else if (item.getShowNum() == 4){
                        circleLayoutManager.setIntervalAngle(20);
                    }
                }
                circleLayoutManager.setShowNum(item.getShowNum());
                recyclerview.setAdapter(item.getAdapter());
            }
            if (recyclerview.getLayoutManager() != item.getLayoutManager()){
                recyclerview.setLayoutManager(item.getLayoutManager());
            }
            FrameLayout.LayoutParams frameLayoutP = (FrameLayout.LayoutParams) recyclerview.getLayoutParams();
            frameLayoutP.width = item.getTopRadius();
            frameLayoutP.height = item.getTopRadius();
            recyclerview.setLayoutParams(frameLayoutP);

        }
}


    public ArcToolRootLyt getArcRootLyt(){
        makeRootLyt();
        return rootLyt;

    }



    public static class AiyouArcToolParams{
        public final Context mContext;
        public int allLevel;
        public int maxRadius;
        public int centerViewBackgroundColor;
        public int centerViewIconResid;
        public int centerviewIconSize;
        public Point centerviewIconPosition;
        public int centerViewRadius;
        public int bgColor;
        public int bgAlpha;
        public List<ArcFadeItem> fadeItems;
        public List<ArcLevelItem> levelItems;
        public AiyouArcMenuItem menuItem;
        public CenterViewClickListener centerViewClickListener;
        public ArcOutsideClickLisitener outsideClickLisitener;
        public ArcOnKeyLisitener onKeyLisitener;

        public AiyouArcToolParams(Context context) {
            this.mContext = context;
        }

        public void apply(ArcToolController controller){
            controller.setAllLevel(allLevel);
            controller.setMaxRadius(maxRadius);
            controller.setCenterViewBackgroundColor(centerViewBackgroundColor);
            controller.setCenterViewIconResid(centerViewIconResid,centerviewIconSize,centerviewIconPosition);
            controller.setCenterViewRadius(centerViewRadius);
            controller.setBgColor(bgColor);
            controller.setBgAlpha(bgAlpha);
            controller.setFadeItems(fadeItems);
            controller.setLevelItems(levelItems);
            controller.setMenuItem(menuItem);
            controller.setCenterViewClickListener(centerViewClickListener);
            controller.setOutsideClickLisitener(outsideClickLisitener);
            controller.setOnKeyLisitener(onKeyLisitener);
        }
    }


}
